package com.errands.cn.services;

import com.errands.cn.entity.Recomment;
import com.errands.cn.mapper.RecommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RecommentService {
    @Autowired
    private RecommentMapper recommentMapper;

    @Transactional
    public void save(Recomment recomment)throws Exception{
        recommentMapper.insert(recomment);
    }

    @Transactional
    public void update(Recomment recomment)throws Exception{
        recommentMapper.updateByPrimaryKeySelective(recomment);
    }

    @Transactional
    public void delete(Integer rId)throws Exception{
        recommentMapper.deleteByPrimaryKey(rId);
    }

    /**
     * 根据订单ID分页获取评论
     * @param oId 订单id
     * @param pageNum 起始页
     * @param pageSize 每页显示数量
     * @return
     */
    public PageInfo<Recomment> findRecommentByOrder(Integer oId,Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Recomment> recomments = recommentMapper.selectByOId(oId);
        PageInfo<Recomment> recommentPageInfo = new PageInfo<>(recomments);
        return recommentPageInfo;
    }

    /**
     * 判断该用户是否评论过该订单
     * @param uid
     * @param oid
     * @return
     */
    public boolean orderRecommentByUserCount(Integer uid,Integer oid){
        int n = recommentMapper.orderRecommentByUserCount(uid,oid);
        if (n == 0)
            return false;
        return true;
    }

}
